package com.example.producer;

import com.example.consumer.pubsub.SysDeptRefreshMessage;
import com.example.consumer.stream.SysMailSendMessage;
import com.example.util.redis.RedisMessageUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/15 10:09
 */
@Component
public class ProducerTest implements CommandLineRunner {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... args) throws Exception {
        CompletableFuture.runAsync(()->testProducer1());
        CompletableFuture.runAsync(()->testProducer2());
    }



    public void testProducer1() {
        while (true){
            // 创建消息
            SysMailSendMessage message = new SysMailSendMessage();
            message.setAddress("50875895@qq.com").setUserType(2).setUserId(1);
            // 发送消息
            RedisMessageUtils.sendStreamMessage(stringRedisTemplate, message);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void testProducer2() {
        while (true){
            // 创建消息
            SysDeptRefreshMessage message = new SysDeptRefreshMessage();
            message.setMsg("这是测试数据啊");
            // 发送消息
            RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
